package com.hc.sort.server.dao;

import java.sql.*;
import java.util.*;
import com.hc.sort.server.pojo.*;
import com.hc.sort.utils.JdbcUtil;

public class BusinessWindowDaoImpl implements BusinessWindowDao {
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	int row = 0;

	@Override
	public int add(BusinessWindow bWindow) {
		String sql = "insert into t_business_window value(null,?,?,?)";

		try {
			conn = JdbcUtil.getConnection();
			pstmt = conn.prepareStatement(sql);

			pstmt.setObject(1, bWindow.getNo());
			pstmt.setObject(2, bWindow.getCode());
			pstmt.setObject(3, bWindow.getName());

			row = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstmt, conn);
		}

		return row;
	}

	@Override
	public int delete(int id) {
		String sql = "delete from t_business_window where bw_id=?";

		try {
			conn = JdbcUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setObject(1, id);

			row = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstmt, conn);
		}

		return row;
	}

	@Override
	public int update(BusinessWindow bWindow) {
		String sql = "update t_business_window set BW_NO=?,BW_TYPE_CODE=?," + "BW_TYPE_NAME=? where bw_id=?";

		try {
			conn = JdbcUtil.getConnection();
			pstmt = conn.prepareStatement(sql);

			pstmt.setObject(1, bWindow.getNo());
			pstmt.setObject(2, bWindow.getCode());
			pstmt.setObject(3, bWindow.getName());
			pstmt.setObject(4, bWindow.getId());

			row = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstmt, conn);
		}
		return row;
	}

	@Override
	public BusinessWindow get(int id) {
		String sql = "select * from t_business_window where bw_id=?";
		try {
			conn = JdbcUtil.getConnection();
			pstmt = conn.prepareStatement(sql);

			// 设置参数
			pstmt.setObject(1, id);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				BusinessWindow bWindow = new BusinessWindow();
				bWindow.setId(rs.getInt("BW_ID"));
				bWindow.setNo(rs.getString("BW_NO"));
				bWindow.setCode(rs.getString("Bw_TYPE_CODE"));
				bWindow.setName(rs.getString("Bw_TYPE_NAME"));

				return bWindow;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstmt, conn);
		}
		return null;
	}

	@Override
	public List<BusinessWindow> findAll() {
		List<BusinessWindow> t = new ArrayList<BusinessWindow>();
		String sql = "select * from t_business_window";
		try {
			conn = JdbcUtil.getConnection();
			pstmt = conn.prepareStatement(sql);

			// 设置参数
			rs = pstmt.executeQuery();

			while (rs.next()) {
				BusinessWindow bWindow = new BusinessWindow();
				bWindow.setId(rs.getInt("Bw_ID"));
				bWindow.setNo(rs.getString("BW_NO"));
				bWindow.setCode(rs.getString("Bw_TYPE_CODE"));
				bWindow.setName(rs.getString("Bw_TYPE_NAME"));

				t.add(bWindow);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstmt, conn);
		}
		return t;
	}

	@Override
	public List<BusinessWindow> searchByKeyword(String keyWord) {
		List<BusinessWindow> busList = new ArrayList<BusinessWindow>();

		String sql = "select * from t_business_window where bw_type_code=? or bw_type_name like ?";
		try {
			conn = JdbcUtil.getConnection();
			pstmt = conn.prepareStatement(sql);

			// 设置参数
			pstmt.setObject(1, keyWord);
			pstmt.setObject(2, '%' + keyWord + '%');
			rs = pstmt.executeQuery();

			while (rs.next()) {
				BusinessWindow bWindow = new BusinessWindow();
				bWindow.setId(rs.getInt("BW_ID"));
				bWindow.setNo(rs.getString("BW_NO"));
				bWindow.setCode(rs.getString("BW_TYPE_CODE"));
				bWindow.setName(rs.getString("BW_TYPE_NAME"));
				busList.add(bWindow);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstmt, conn);
		}
		return busList;
	}

	public static void main(String[] args) {
		BusinessWindowDao bDao = new BusinessWindowDaoImpl();

//		BusinessWindow b=new BusinessWindow("04","A","vip业务");
//		System.out.println(bDao.add(b));

//		System.out.println(bDao.delete(2));

//		BusinessWindow b = new BusinessWindow("03", "B", "对公业务", 1);
//		System.out.println(bDao.update(b));

//		BusinessWindow b=bDao.get(1);
//		System.out.println(b);

		// 查询全部信息
//		List<BusinessWindow> list=bDao.findAll();
//		System.out.println(list);
		
		System.out.println(bDao.searchByKeyword("A"));
	}
}
